::: {.cell}

Code
library(tidyverse)

:::

1 vector

A vector is an ordered collection of basic data types of a given length. The only key thing here is all the elements of a vector must be same data type e.g homogeneous data structures. Vectors are one-dimensional data structures.

Code
a = c(1, 2, 3, 4)
a
[1] 1 2 3 4
Code
class(a)
[1] "numeric"
Code
b =  c("Debi", "Sandeep", "Subham", "Shiba")
b
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  
Code
class(b)
[1] "character"

1.1 create sequence vector

Code
seq(from = 2, to = 14, by = 2) 
[1]  2  4  6  8 10 12 14

1.2 create repeat vector

Code
rep(x = 1.5, times = 4)  
[1] 1.5 1.5 1.5 1.5

1.3 create random vector

create 5 random number from 1 to 10 without replacement

Code
sample(1:10,5, replace=F) 
[1] 6 7 2 1 8

create 5 random number from 1 to 10 with replacement

Code
sample(1:10,5, replace=T) 
[1] 4 6 6 8 5

create 1 random number from 0 to 1 from random uniform distribution

Code
runif(1, min=0, max=1)
[1] 0.5690518

generate 4 random number that follows the normal distribution with mean being 0 and standard deviation being 1

Code
sn1 <- rnorm(4, mean=0, sd=1) # standard nromal
sn1
[1]  0.59119881  0.14159089 -0.43121657  0.04111442

1.4 create unique vector

Code
v1=c(1,1,2,2,5,6)
v1
[1] 1 1 2 2 5 6
Code
unique(v1)
[1] 1 2 5 6

1.5 append vector

Code
x=c(1,2,3)
y=c(4,5,6)
z=c(x,y)
z
[1] 1 2 3 4 5 6

1.6 remove element in vector

Code
x=c(1,2,3,4,5)
x
[1] 1 2 3 4 5

1.6.1 remove first one

Code
x[-1]
[1] 2 3 4 5

1.6.2 remove last one

Code
x[-length(x)]
[1] 1 2 3 4

1.6.3 remove from last second

Code
x[1:(length(x)-2)]
[1] 1 2 3

1.6.4 remove from from another vector

Code
remove=c(2,4)
x[-remove]
[1] 1 3 5

1.7 sort vector

Code
a=c(2,4,6,1,4)
Code
sort(a)
[1] 1 2 4 4 6
Code
sort(a,decreasing=TRUE)
[1] 6 4 4 2 1

1.8 vector length

Code
length(a)
[1] 5

1.9 calculate vector

Code
x=c(1,2,3,4,5)

sum(x)
[1] 15

1.10 select vector element

Code
x=c(1,2,3,6,9,10)

1.10.1 select first

Code
x[1]
[1] 1

or

Code
x %>% nth(1)
[1] 1

1.10.2 select last

Code
x %>% last()
[1] 10

or

Code
x %>% nth(-1)
[1] 10

1.10.3 select second last

Code
x %>% nth(-2)
[1] 9

1.10.4 select first to 3th

Code
x[1:3]
[1] 1 2 3

1.10.5 select last one to last 3th

Code
x[-3:-1]
[1]  6  9 10

1.11 compare two vector

Code
xx=c(1,2,3,4)
xx
[1] 1 2 3 4
Code
yy=c(2,4)
yy
[1] 2 4

find number only in xx not in yy

Code
setdiff(xx, yy)
[1] 1 3

1.12 vector Converting between types

1.12.1 to factor

Code
x <- c("a", "g", "b")
y=as.factor(x)
y
[1] a g b
Levels: a b g

1.12.2 to numeric

Code
x <- c('123','44', '222')
y=as.numeric(x)
y
[1] 123  44 222

1.12.3 to character

Code
x <- c(123123,111,5555)
y=as.character(x)
y
[1] "123123" "111"    "5555"  

1.12.4 to boolen

Code
x <- c(1,0,1)
y=as.logical(x)
y
[1]  TRUE FALSE  TRUE

1.13 vector to other data format

1.13.1 vector to dataframe

Code
Name <- c("Jhon", "Lee", "Suzan", "Abhinav", 
          "Brain", "Emma", "David", "Alice") 

   
Marks <- c(56, 76, 86, 96, 73, 87, 47, 98) 
    

data<- data.frame(Name,Marks) 
data
     Name Marks
1    Jhon    56
2     Lee    76
3   Suzan    86
4 Abhinav    96
5   Brain    73
6    Emma    87
7   David    47
8   Alice    98
Code
class(data)
[1] "data.frame"

1.13.2 verctor to matrix

Code
v1 <- c(56, 76, 86, 96, 73, 87, 47, 98) 
Code
data<-matrix(v1,nrow=4)
data
     [,1] [,2]
[1,]   56   73
[2,]   76   87
[3,]   86   47
[4,]   96   98
Code
class(data)
[1] "matrix" "array" 

other way: ::: {.cell}

Code
data<-matrix(c(v1,v1),ncol = 2)
data
     [,1] [,2]
[1,]   56   56
[2,]   76   76
[3,]   86   86
[4,]   96   96
[5,]   73   73
[6,]   87   87
[7,]   47   47
[8,]   98   98

:::

1.13.3 verctor to list

Code
data=list(v1)
data
[[1]]
[1] 56 76 86 96 73 87 47 98
Code
class(data)
[1] "list"

each element in vetor as a element in list

Code
data=as.list(v1)
data
[[1]]
[1] 56

[[2]]
[1] 76

[[3]]
[1] 86

[[4]]
[1] 96

[[5]]
[1] 73

[[6]]
[1] 87

[[7]]
[1] 47

[[8]]
[1] 98

1.13.4 verctor to array

3D array ::: {.cell}

Code
vec1=c(1:5) 
 
vec2=c(6:10) 
 
arr=array(c(vec1,vec2),dim=c(2,5,3)) 
 
# printing the array
class(arr)
[1] "array"

:::

Code
arr
, , 1

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 2

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

, , 3

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    3    5    7    9
[2,]    2    4    6    8   10

2 Dataframe

Dataframes are generic data objects of R which are used to store the tabular data. Dataframes are the foremost popular data objects in R programming because we are comfortable in seeing the data within the tabular form. They are two-dimensional, heterogeneous data structures

Code
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")

# A vector which is a character vector
Language = c("R", "Python", "Java")

# A vector which is a numeric vector
Age = c(22, 25, 45)

# To create dataframe use data.frame command
# and then pass each of the vectors 
# we have created as arguments
# to the function data.frame()
df = data.frame(Name, Language, Age)

df
   Name Language Age
1 Amiya        R  22
2   Raj   Python  25
3 Asish     Java  45
Code
class(df)
[1] "data.frame"

2.1 dataframe to other data format

2.1.1 dataframe to matrix

Code
mat <- as.matrix(df)

class(mat)
[1] "matrix" "array" 
Code
mat
     Name    Language Age 
[1,] "Amiya" "R"      "22"
[2,] "Raj"   "Python" "25"
[3,] "Asish" "Java"   "45"

2.1.2 dataframe to vector

Code
vec=df[['Name']]

class(vec)
[1] "character"
Code
vec
[1] "Amiya" "Raj"   "Asish"

2.1.3 dataframe to list

Code
list=as.list(df[['Name']])

class(list)
[1] "list"
Code
list
[[1]]
[1] "Amiya"

[[2]]
[1] "Raj"

[[3]]
[1] "Asish"

3 Matrices

A matrix is a rectangular arrangement of numbers in rows and columns. In a matrix, as we know rows are the ones that run horizontally and columns are the ones that run vertically. Matrices are two-dimensional, homogeneous data structures.

Code
A = matrix(
    # Taking sequence of elements
    c(1, 2, 3, 4, 5, 6, 7, 8, 9), 
    
    # No of rows and columns
    nrow = 3, ncol = 3,  

    # By default matrices are 
    # in column-wise order 
    # So this parameter decides
    # how to arrange the matrix          
    byrow = TRUE                             
)

A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
Code
class(A)
[1] "matrix" "array" 
Code
matrix002=A+A

matrix002
     [,1] [,2] [,3]
[1,]    2    4    6
[2,]    8   10   12
[3,]   14   16   18
Code
matrix003=A*A

matrix003
     [,1] [,2] [,3]
[1,]    1    4    9
[2,]   16   25   36
[3,]   49   64   81

3.1 matrix to other data format

3.1.1 matrix to dataframe

Code
df <- as.data.frame(matrix003)

class(df)
[1] "data.frame"
Code
df
  V1 V2 V3
1  1  4  9
2 16 25 36
3 49 64 81

3.1.2 matrix to vector

Code
vec=as.vector(matrix003)

class(vec)
[1] "numeric"
Code
vec
[1]  1 16 49  4 25 64  9 36 81

3.1.3 matrix to list

Code
list=as.list(matrix003)

class(list)
[1] "list"
Code
list
[[1]]
[1] 1

[[2]]
[1] 16

[[3]]
[1] 49

[[4]]
[1] 4

[[5]]
[1] 25

[[6]]
[1] 64

[[7]]
[1] 9

[[8]]
[1] 36

[[9]]
[1] 81

4 Lists

A list is a generic object consisting of an ordered collection of objects. Lists are heterogeneous data structures. These are also one-dimensional data structures. A list can be a list of vectors, list of matrices, a list of characters and a list of functions and so on.

Code
empId = c(1, 2, 3, 4)

# The second attribute is the employee name 
# which is created using this line of code here
# which is the character vector 
empName = c("Debi", "Sandeep", "Subham", "Shiba")

# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4

# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)

empList
[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

[[3]]
[1] 4
Code
class(empList)
[1] "list"

5 Arrays

3D arrays

Code
A = array(
    # Taking sequence of elements
    c(1, 2, 3, 4, 5, 6, 7, 8),

    # Creating two rectangular matrices 
    # each with two rows and two columns
    dim = c(2, 2, 2)                        
)

A
, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8
Code
class(A)
[1] "array"

6 Reference:

https://www.geeksforgeeks.org/data-structures-in-r-programming/

Back to top